package com.zhentao.dataintegrity.util;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * Excel 导出工具类
 * 用于数据完整性检查功能的数据导出
 */
public class ExcelExportUtil {

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * 导出任务列表到 Excel
     */
    public static byte[] exportTaskList(List<TaskExportData> taskList) throws IOException {
        try (Workbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet("完整性检查任务列表");
            
            // 创建表头样式
            CellStyle headerStyle = createHeaderStyle(workbook);
            
            // 创建表头
            Row headerRow = sheet.createRow(0);
            String[] headers = {"序号", "标题", "最后运行时间", "下次运行时间", "状态", "最后差异人民币金额", "最后差异美元金额"};
            for (int i = 0; i < headers.length; i++) {
                Cell cell = headerRow.createCell(i);
                cell.setCellValue(headers[i]);
                cell.setCellStyle(headerStyle);
            }
            
            // 填充数据
            for (int i = 0; i < taskList.size(); i++) {
                Row row = sheet.createRow(i + 1);
                TaskExportData task = taskList.get(i);
                
                row.createCell(0).setCellValue(i + 1);
                row.createCell(1).setCellValue(task.getTaskTitle());
                row.createCell(2).setCellValue(task.getLastRunTime() != null ? 
                    task.getLastRunTime().format(DATE_TIME_FORMATTER) : "");
                row.createCell(3).setCellValue(task.getNextRunTime() != null ? 
                    task.getNextRunTime().format(DATE_TIME_FORMATTER) : "");
                row.createCell(4).setCellValue(task.getTaskStatus());
                row.createCell(5).setCellValue(task.getLastDiffRmb() != null ? 
                    task.getLastDiffRmb().doubleValue() : 0.0);
                row.createCell(6).setCellValue(task.getLastDiffUsd() != null ? 
                    task.getLastDiffUsd().doubleValue() : 0.0);
            }
            
            // 自动调整列宽
            for (int i = 0; i < headers.length; i++) {
                sheet.autoSizeColumn(i);
            }
            
            // 转换为字节数组
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            workbook.write(outputStream);
            return outputStream.toByteArray();
        }
    }

    /**
     * 导出子任务详情到 Excel
     */
    public static byte[] exportSubtaskDetails(List<SubtaskExportData> subtaskList) throws IOException {
        try (Workbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet("子任务详情列表");
            
            // 创建表头样式
            CellStyle headerStyle = createHeaderStyle(workbook);
            
            // 创建表头
            Row headerRow = sheet.createRow(0);
            String[] headers = {"序号", "数据类型", "执行状态", "执行时间", "数据范围", "差异人民币金额", "差异美元金额"};
            for (int i = 0; i < headers.length; i++) {
                Cell cell = headerRow.createCell(i);
                cell.setCellValue(headers[i]);
                cell.setCellStyle(headerStyle);
            }
            
            // 填充数据
            for (int i = 0; i < subtaskList.size(); i++) {
                Row row = sheet.createRow(i + 1);
                SubtaskExportData subtask = subtaskList.get(i);
                
                row.createCell(0).setCellValue(i + 1);
                row.createCell(1).setCellValue(subtask.getDataType());
                row.createCell(2).setCellValue(subtask.getExecutionStatus());
                row.createCell(3).setCellValue(subtask.getStartTime() != null ? 
                    subtask.getStartTime().format(DATE_TIME_FORMATTER) : "");
                row.createCell(4).setCellValue(subtask.getDataRangeStart() + " 至 " + subtask.getDataRangeEnd());
                row.createCell(5).setCellValue(subtask.getDiffRmbAmount() != null ? 
                    subtask.getDiffRmbAmount().doubleValue() : 0.0);
                row.createCell(6).setCellValue(subtask.getDiffUsdAmount() != null ? 
                    subtask.getDiffUsdAmount().doubleValue() : 0.0);
            }
            
            // 自动调整列宽
            for (int i = 0; i < headers.length; i++) {
                sheet.autoSizeColumn(i);
            }
            
            // 转换为字节数组
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            workbook.write(outputStream);
            return outputStream.toByteArray();
        }
    }

    /**
     * 创建表头样式
     */
    private static CellStyle createHeaderStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        
        // 设置背景色
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        
        // 设置边框
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        
        // 设置字体
        Font font = workbook.createFont();
        font.setBold(true);
        style.setFont(font);
        
        // 设置对齐方式
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        
        return style;
    }

    /**
     * 任务导出数据类
     */
    public static class TaskExportData {
        private String taskTitle;
        private LocalDateTime lastRunTime;
        private LocalDateTime nextRunTime;
        private String taskStatus;
        private java.math.BigDecimal lastDiffRmb;
        private java.math.BigDecimal lastDiffUsd;

        // Getters and Setters
        public String getTaskTitle() { return taskTitle; }
        public void setTaskTitle(String taskTitle) { this.taskTitle = taskTitle; }

        public LocalDateTime getLastRunTime() { return lastRunTime; }
        public void setLastRunTime(LocalDateTime lastRunTime) { this.lastRunTime = lastRunTime; }

        public LocalDateTime getNextRunTime() { return nextRunTime; }
        public void setNextRunTime(LocalDateTime nextRunTime) { this.nextRunTime = nextRunTime; }

        public String getTaskStatus() { return taskStatus; }
        public void setTaskStatus(String taskStatus) { this.taskStatus = taskStatus; }

        public java.math.BigDecimal getLastDiffRmb() { return lastDiffRmb; }
        public void setLastDiffRmb(java.math.BigDecimal lastDiffRmb) { this.lastDiffRmb = lastDiffRmb; }

        public java.math.BigDecimal getLastDiffUsd() { return lastDiffUsd; }
        public void setLastDiffUsd(java.math.BigDecimal lastDiffUsd) { this.lastDiffUsd = lastDiffUsd; }
    }

    /**
     * 子任务导出数据类
     */
    public static class SubtaskExportData {
        private String dataType;
        private String executionStatus;
        private LocalDateTime startTime;
        private String dataRangeStart;
        private String dataRangeEnd;
        private java.math.BigDecimal diffRmbAmount;
        private java.math.BigDecimal diffUsdAmount;

        // Getters and Setters
        public String getDataType() { return dataType; }
        public void setDataType(String dataType) { this.dataType = dataType; }

        public String getExecutionStatus() { return executionStatus; }
        public void setExecutionStatus(String executionStatus) { this.executionStatus = executionStatus; }

        public LocalDateTime getStartTime() { return startTime; }
        public void setStartTime(LocalDateTime startTime) { this.startTime = startTime; }

        public String getDataRangeStart() { return dataRangeStart; }
        public void setDataRangeStart(String dataRangeStart) { this.dataRangeStart = dataRangeStart; }

        public String getDataRangeEnd() { return dataRangeEnd; }
        public void setDataRangeEnd(String dataRangeEnd) { this.dataRangeEnd = dataRangeEnd; }

        public java.math.BigDecimal getDiffRmbAmount() { return diffRmbAmount; }
        public void setDiffRmbAmount(java.math.BigDecimal diffRmbAmount) { this.diffRmbAmount = diffRmbAmount; }

        public java.math.BigDecimal getDiffUsdAmount() { return diffUsdAmount; }
        public void setDiffUsdAmount(java.math.BigDecimal diffUsdAmount) { this.diffUsdAmount = diffUsdAmount; }
    }
}
